<?php

namespace app\controllers\merchant\shop;

use app\models\merchant\system\OperationRecordModel;
use app\models\merchant\user\MerchantModel;
use app\models\shop\CollectModel;
use app\models\shop\SaleGoodsModel;
use app\models\shop\SaleGoodsStockModel;
use yii;
use yii\db\Exception;
use yii\web\MerchantController;

/**
 * 应用类目表控制器
 * 地址:/admin/rule
 * @throws Exception if the model cannot be found
 * @return array
 */
class CollectController extends MerchantController {

    public $enableCsrfValidation = false; //禁用CSRF令牌验证，可以在基类中设置

    /**
     * 地址:/admin/group/index 默认访问
     * @throws Exception if the model cannot be found
     * @return array
     */

    public function actionList() {
        if (yii::$app->request->isGet) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->get(); //获取地址栏参数

            $must = ['key'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }
            $model = new CollectModel();
            $where['key'] = $params['key'];
            $where['merchant_id'] = yii::$app->session['uid'];
            $array = $model->do_one($where);
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }

    //apiKey配置
    public function actionConfig() {
        if (yii::$app->request->isPost) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参

            $must = ['key','apikey'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new CollectModel();
            $info = $model->do_one([]);
            if ($info['status'] == 200) {
                $where['id'] = $info['data']['id'];
                $data['apikey'] = $params['apikey'];
                $array = $model->do_update($where,$data);
                $operationRecordData['operation_type'] = '更新';
                $operationRecordData['operation_id'] = $info['data']['id'];
            } else {
                $data['key'] = $params['key'];
                $data['merchant_id'] = yii::$app->session['uid'];
                $data['apikey'] = $params['apikey'];
                $array = $model->do_add($data);
                $operationRecordData['operation_type'] = '新增';
                $operationRecordData['operation_id'] = $array['data'];
            }
            if ($array['status'] == 200){
                //添加操作记录
                $operationRecordModel = new OperationRecordModel();
                $operationRecordData['key'] = $params['key'];
                if (isset(yii::$app->session['sid'])) {
                    $subModel = new \app\models\merchant\system\UserModel();
                    $subInfo = $subModel->find(['id'=>yii::$app->session['sid']]);
                    if ($subInfo['status'] == 200){
                        $operationRecordData['merchant_id'] = $subInfo['data']['username'];
                    }
                } else {
                    $merchantModle = new MerchantModel();
                    $merchantInfo = $merchantModle->find(['id'=>yii::$app->session['uid']]);
                    if ($merchantInfo['status'] == 200) {
                        $operationRecordData['merchant_id'] = $merchantInfo['data']['name'];
                    }
                }
                $operationRecordData['module_name'] = '商品采集';
                $operationRecordModel->do_add($operationRecordData);
            }
            return $array;
        } else {
            return result(500, "请求方式错误");
        }
    }



    public function actionIndex() {
        if (yii::$app->request->isPost) {
            $request = yii::$app->request; //获取 request 对象
            $params = $request->bodyParams; //获取body传参

            $must = ['key','itemid','type'];
            $rs = $this->checkInput($must, $params);
            if ($rs != false) {
                return $rs;
            }

            $model = new CollectModel();
            $info = $model->do_one([]);
            if ($info['status'] != 200) {
                return result(500, "apikey未设置");
            }
            $apiKey = $info['data']['apikey'];

            switch ($params['type'])
            {
                case "1":
                    $res = $this->tb($params['key'],$apiKey,$params['itemid']);
                    break;
                case "2":
                    $res = $this->tm($params['key'],$apiKey,$params['itemid']);
                    break;
                case "3":
                    $res = $this->jd($params['key'],$apiKey,$params['itemid']);
                    break;
                case "4":
                    $res = $this->pf1688($params['key'],$apiKey,$params['itemid']);
                    break;
                default:
                    return result(500, "type类型错误");
            }


            return $res;
        } else {
            return result(500, "请求方式错误");
        }
    }


    function tb($key,$apiKey,$itemid){
        $url = "https://api03.6bqb.com/taobao/detail?apikey={$apiKey}&itemid={$itemid}";
        $res = json_decode(curlGet($url),true);

        if ($res['retcode'] === '0000') {
            $goodsModle = new SaleGoodsModel();
            $goodsData['key'] = $key;
            $goodsData['merchant_id'] = yii::$app->session['uid'];
            $goodsData['name'] = $res['data']['item']['title'];
            $goodsData['short_name'] = $res['data']['item']['subTitle'];
            $goodsData['attribute'] = [];
            $goodsData['attribute'] = json_encode($goodsData['attribute']);
            if (isset($res['data']['item']['images']) && is_array($res['data']['item']['images'])){
                $imgNum = count($res['data']['item']['images']);
                $imgNum = $imgNum > 5 ? 5 : $imgNum;
                $goodsData['pic_urls'] = '';
                for ($i = 0;$i < $imgNum;$i++) {
                    if ($i == 0) {
                        $goodsData['pic_urls'] .= 'http:'.$res['data']['item']['images'][$i];
                    } else {
                        $goodsData['pic_urls'] .= ',http:'.$res['data']['item']['images'][$i];
                    }
                }
            }
            if (isset($res['data']['item']['props']) && is_array($res['data']['item']['props'])) {
                $specsNum = count($res['data']['item']['props']);
                if ($specsNum == 1) { //单规格
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['props'][0]['name'].':';
                    $goodsData['property2'] = '';
                    for ($i = 0;$i < count($res['data']['item']['props'][0]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['props'][0]['values'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['props'][0]['values'][$i]['name'];
                        }
                    }
                } elseif ($specsNum == 2) { //双规格
                    $goodsData['stock_type'] = 2;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['props'][0]['name'].':';
                    for ($i = 0;$i < count($res['data']['item']['props'][0]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['props'][0]['values'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['props'][0]['values'][$i]['name'];
                        }
                    }
                    $goodsData['property2'] = $res['data']['item']['props'][1]['name'].':';
                    for ($i = 0;$i < count($res['data']['item']['props'][1]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property2'] .= $res['data']['item']['props'][1]['values'][$i]['name'];
                        } else {
                            $goodsData['property2'] .= ','.$res['data']['item']['props'][1]['values'][$i]['name'];
                        }
                    }
                } else {
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = '默认:默认';
                    $goodsData['property2'] = '';
                }
            }else{
                $goodsData['stock_type'] = 1;
                $goodsData['have_stock_type'] = 1;
                $goodsData['property1'] = '默认:默认';
                $goodsData['property2'] = '';
            }
            if (isset($res['data']['item']['descImgs']) && is_array($res['data']['item']['descImgs'])){
                $goodsData['detail_info'] = '<p>';
                foreach ($res['data']['item']['descImgs'] as $k=>$v){
                    $goodsData['detail_info'] .= "<img class='wscnph' style='width: 100%; vertical-align: middle;' src='{$v}' />";
                }
                $goodsData['detail_info'] .= '</p>';
            }else{
                $goodsData['detail_info'] = '<p>   </p>';
            }
            if (isset($res['data']['item']['priceRange'])){
                $price = explode("-", $res['data']['item']['priceRange']);
                $goodsData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
            }else{
                $goodsData['price'] = 0;
            }
            if (isset($res['data']['item']['sku']) && is_array($res['data']['item']['sku'])){
                $goodsData['stocks'] = $res['data']['item']['sku'][0]['quantity'];
            } else {
                $goodsData['stocks'] = 0;
            }
            $array = $goodsModle->do_add($goodsData);
            if ($array['status'] == 200){
                $stockModel = new SaleGoodsStockModel();
                $stockData['key'] = $key;
                $stockData['merchant_id'] = yii::$app->session['uid'];
                $stockData['goods_id'] = $array['data'];
                $stockData['name'] = $res['data']['item']['title'];
                $stockData['status'] = 1;
                if (isset($res['data']['item']['props']) && is_array($res['data']['item']['props'])){
                    if ($specsNum == 1) {
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $stockData['property1_name'] = substr($res['data']['item']['sku'][$i]['skuName'],strripos($res['data']['item']['sku'][$i]['skuName'],"-")+1);
                            $stockData['property2_name'] = '';
                            $stockData['number'] = $res['data']['item']['sku'][$i]['quantity'];
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = 'http:'.$res['data']['item']['sku'][$i]['image'];
                            $stockModel->do_add($stockData);
                        }
                    } elseif ($specsNum == 2) {
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $skuName = explode(";", $res['data']['item']['sku'][$i]['skuName']);
                            $stockData['property1_name'] = substr($skuName[0],strripos($skuName[0],"-")+1);
                            $stockData['property2_name'] = substr($skuName[1],strripos($skuName[1],"-")+1);
                            $stockData['number'] = $res['data']['item']['sku'][$i]['quantity'];
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = 'http:'.$res['data']['item']['sku'][$i]['image'];
                            $stockModel->do_add($stockData);
                        }
                    } else {
                        $stockData['property1_name'] = '默认';
                        $stockData['property2_name'] = '';
                        $stockData['number'] = 0;
                        if (isset($res['data']['item']['priceRange'])){
                            $price = explode("-", $res['data']['item']['priceRange']);
                            $stockData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
                        }else{
                            $stockData['price'] = 0;
                        }
                        $stockData['pic_url'] = 'http:'.$res['data']['item']['images'][0];
                        $stockModel->do_add($stockData);
                    }
                }else{
                    $stockData['property1_name'] = '默认';
                    $stockData['property2_name'] = '';
                    $stockData['number'] = 0;
                    if (isset($res['data']['item']['priceRange'])){
                        $price = explode("-", $res['data']['item']['priceRange']);
                        $stockData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
                    }else{
                        $stockData['price'] = 0;
                    }
                    $stockData['pic_url'] = 'http:'.$res['data']['item']['images'][0];
                    $stockModel->do_add($stockData);
                }

            }
            return $array;
        } else {
            return result(500,'商品:'.$itemid.',导入失败');
        }
    }

    function tm($key,$apiKey,$itemid){
        $url = "https://api03.6bqb.com/tmall/detail?apikey={$apiKey}&itemid={$itemid}";
        $res = json_decode(curlGet($url),true);

        if ($res['retcode'] === '0000') {
            $goodsModle = new SaleGoodsModel();
            $goodsData['key'] = $key;
            $goodsData['merchant_id'] = yii::$app->session['uid'];
            $goodsData['name'] = $res['data']['item']['title'];
            $goodsData['short_name'] = $res['data']['item']['subTitle'];
            $goodsData['attribute'] = [];
            $goodsData['attribute'] = json_encode($goodsData['attribute']);
            if (isset($res['data']['item']['images']) && is_array($res['data']['item']['images'])){
                $imgNum = count($res['data']['item']['images']);
                $imgNum = $imgNum > 5 ? 5 : $imgNum;
                $goodsData['pic_urls'] = '';
                for ($i = 0;$i < $imgNum;$i++) {
                    if ($i == 0) {
                        $goodsData['pic_urls'] .= 'http:'.$res['data']['item']['images'][$i];
                    } else {
                        $goodsData['pic_urls'] .= ',http:'.$res['data']['item']['images'][$i];
                    }
                }
            }
            if (isset($res['data']['item']['props']) && is_array($res['data']['item']['props'])) {
                $specsNum = count($res['data']['item']['props']);
                if ($specsNum == 1) { //单规格
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['props'][0]['name'].':';
                    $goodsData['property2'] = '';
                    for ($i = 0;$i < count($res['data']['item']['props'][0]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['props'][0]['values'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['props'][0]['values'][$i]['name'];
                        }
                    }
                } elseif ($specsNum == 2) { //双规格
                    $goodsData['stock_type'] = 2;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['props'][0]['name'].':';
                    for ($i = 0;$i < count($res['data']['item']['props'][0]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['props'][0]['values'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['props'][0]['values'][$i]['name'];
                        }
                    }
                    $goodsData['property2'] = $res['data']['item']['props'][1]['name'].':';
                    for ($i = 0;$i < count($res['data']['item']['props'][1]['values']);$i++) {
                        if ($i == 0) {
                            $goodsData['property2'] .= $res['data']['item']['props'][1]['values'][$i]['name'];
                        } else {
                            $goodsData['property2'] .= ','.$res['data']['item']['props'][1]['values'][$i]['name'];
                        }
                    }
                } else {
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = '默认:默认';
                    $goodsData['property2'] = '';
                }
            }else{
                $goodsData['stock_type'] = 1;
                $goodsData['have_stock_type'] = 1;
                $goodsData['property1'] = '默认:默认';
                $goodsData['property2'] = '';
            }
            if (isset($res['data']['item']['descImgs']) && is_array($res['data']['item']['descImgs'])){
                $goodsData['detail_info'] = '<p>';
                foreach ($res['data']['item']['descImgs'] as $k=>$v){
                    $goodsData['detail_info'] .= "<img class='wscnph' style='width: 100%; vertical-align: middle;' src='{$v}' />";
                }
                $goodsData['detail_info'] .= '</p>';
            }else{
                $goodsData['detail_info'] = '<p>   </p>';
            }
            if (isset($res['data']['item']['priceRange'])){
                $price = explode("-", $res['data']['item']['priceRange']);
                $goodsData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
            }else{
                $goodsData['price'] = 0;
            }
            if (isset($res['data']['item']['sku']) && is_array($res['data']['item']['sku'])){
                $goodsData['stocks'] = $res['data']['item']['sku'][0]['quantity'];
            } else {
                $goodsData['stocks'] = 0;
            }
            $array = $goodsModle->do_add($goodsData);
            if ($array['status'] == 200){
                $stockModel = new SaleGoodsStockModel();
                $stockData['key'] = $key;
                $stockData['merchant_id'] = yii::$app->session['uid'];
                $stockData['goods_id'] = $array['data'];
                $stockData['name'] = $res['data']['item']['title'];
                $stockData['status'] = 1;
                if (isset($res['data']['item']['props']) && is_array($res['data']['item']['props'])){
                    if ($specsNum == 1) {
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $stockData['property1_name'] = substr($res['data']['item']['sku'][$i]['skuName'],strripos($res['data']['item']['sku'][$i]['skuName'],"-")+1);
                            $stockData['property2_name'] = '';
                            $stockData['number'] = $res['data']['item']['sku'][$i]['quantity'];
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = 'http:'.$res['data']['item']['sku'][$i]['image'];
                            $stockModel->do_add($stockData);
                        }
                    } elseif ($specsNum == 2) {
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $skuName = explode(";", $res['data']['item']['sku'][$i]['skuName']);
                            $stockData['property1_name'] = substr($skuName[0],strripos($skuName[0],"-")+1);
                            $stockData['property2_name'] = substr($skuName[1],strripos($skuName[1],"-")+1);
                            $stockData['number'] = $res['data']['item']['sku'][$i]['quantity'];
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = 'http:'.$res['data']['item']['sku'][$i]['image'];
                            $stockModel->do_add($stockData);
                        }
                    } else {
                        $stockData['property1_name'] = '默认';
                        $stockData['property2_name'] = '';
                        $stockData['number'] = 0;
                        if (isset($res['data']['item']['priceRange'])){
                            $price = explode("-", $res['data']['item']['priceRange']);
                            $stockData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
                        }else{
                            $stockData['price'] = 0;
                        }
                        $stockData['pic_url'] = 'http:'.$res['data']['item']['images'][0];
                        $stockModel->do_add($stockData);
                    }
                }else{
                    $stockData['property1_name'] = '默认';
                    $stockData['property2_name'] = '';
                    $stockData['number'] = 0;
                    if (isset($res['data']['item']['priceRange'])){
                        $price = explode("-", $res['data']['item']['priceRange']);
                        $stockData['price'] = is_array($price) ? $price[0] : $res['data']['item']['priceRange'];
                    }else{
                        $stockData['price'] = 0;
                    }
                    $stockData['pic_url'] = 'http:'.$res['data']['item']['images'][0];
                    $stockModel->do_add($stockData);
                }

            }
            return $array;
        } else {
            return result(500,'商品:'.$itemid.',导入失败');
        }
    }

    function jd($key,$apiKey,$itemid){
        $url = "https://api03.6bqb.com/jd/detail?apikey={$apiKey}&itemid={$itemid}";
        $res = json_decode(curlGet($url),true);

        if ($res['retcode'] === '0000') {
            $goodsModle = new SaleGoodsModel();
            $goodsData['key'] = $key;
            $goodsData['merchant_id'] = yii::$app->session['uid'];
            $goodsData['name'] = $res['data']['item']['name'];
            $goodsData['attribute'] = [];
            $goodsData['attribute'] = json_encode($goodsData['attribute']);
            if (isset($res['data']['item']['images']) && is_array($res['data']['item']['images'])){
                $imgNum = count($res['data']['item']['images']);
                $imgNum = $imgNum > 5 ? 5 : $imgNum;
                $goodsData['pic_urls'] = '';
                for ($i = 0;$i < $imgNum;$i++) {
                    if ($i == 0) {
                        $goodsData['pic_urls'] .= $res['data']['item']['images'][$i];
                    } else {
                        $goodsData['pic_urls'] .= ','.$res['data']['item']['images'][$i];
                    }
                }
            }
            if (isset($res['data']['item']['skuProps']) && is_array($res['data']['item']['skuProps'])) {
                if (!empty($res['data']['item']['skuProps'][3][0])) { //三种规格
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = '默认:默认';
                    $goodsData['property2'] = '';
                } elseif (!empty($res['data']['item']['skuProps'][2][0])) { //双规格
                    $goodsData['stock_type'] = 2;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['saleProp'][1].':';
                    for ($i = 0;$i < count($res['data']['item']['skuProps'][1]);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['skuProps'][1][$i];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['skuProps'][1][$i];
                        }
                    }
                    $goodsData['property2'] = $res['data']['item']['saleProp'][2].':';
                    for ($i = 0;$i < count($res['data']['item']['skuProps'][2]);$i++) {
                        if ($i == 0) {
                            $goodsData['property2'] .= $res['data']['item']['skuProps'][2][$i];
                        } else {
                            $goodsData['property2'] .= ','.$res['data']['item']['skuProps'][2][$i];
                        }
                    }
                } elseif (!empty($res['data']['item']['skuProps'][1][0])) {
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['item']['saleProp'][1].':';
                    $goodsData['property2'] = '';
                    for ($i = 0;$i < count($res['data']['item']['skuProps'][1]);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['item']['skuProps'][1][$i];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['item']['skuProps'][1][$i];
                        }
                    }
                } else {
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = '默认:默认';
                    $goodsData['property2'] = '';
                }
            }else{
                $goodsData['stock_type'] = 1;
                $goodsData['have_stock_type'] = 1;
                $goodsData['property1'] = '默认:默认';
                $goodsData['property2'] = '';
            }
            if (isset($res['data']['item']['descImgs']) && is_array($res['data']['item']['descImgs'])){
                $goodsData['detail_info'] = '<p>';
                foreach ($res['data']['item']['descImgs'] as $k=>$v){
                    $goodsData['detail_info'] .= "<img class='wscnph' style='width: 100%; vertical-align: middle;' src='{$v}' />";
                }
                $goodsData['detail_info'] .= '</p>';
            }else{
                $goodsData['detail_info'] = '<p>   </p>';
            }
            if (isset($res['data']['item']['price'])){
                $goodsData['price'] = $res['data']['item']['price'];
            }else{
                $goodsData['price'] = 0;
            }
            if (isset($res['data']['item']['originalPrice'])){
                $goodsData['line_price'] = $res['data']['item']['originalPrice'];
            }
            $goodsData['stocks'] = 0;
            $array = $goodsModle->do_add($goodsData);
            if ($array['status'] == 200){
                $stockModel = new SaleGoodsStockModel();
                $stockData['key'] = $key;
                $stockData['merchant_id'] = yii::$app->session['uid'];
                $stockData['goods_id'] = $array['data'];
                $stockData['name'] = $res['data']['item']['name'];
                $stockData['status'] = 1;
                if (isset($res['data']['item']['sku']) && is_array($res['data']['item']['sku'])){
                    if (!empty($res['data']['item']['skuProps'][3][0])) {
                        $stockData['property1_name'] = '默认';
                        $stockData['property2_name'] = '';
                        $stockData['number'] = 0;
                        if (isset($res['data']['item']['price'])){
                            $stockData['price'] = $res['data']['item']['price'];
                        }else{
                            $stockData['price'] = 0;
                        }
                        $stockData['pic_url'] = $res['data']['item']['images'][0];
                        $stockModel->do_add($stockData);
                    } elseif (!empty($res['data']['item']['skuProps'][2][0])) {
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $stockData['property1_name'] = $res['data']['item']['sku'][$i][1];
                            $stockData['property2_name'] = $res['data']['item']['sku'][$i][2];
                            $stockData['number'] = 0;
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = $res['data']['item']['images'][0];
                            $stockModel->do_add($stockData);
                        }
                    } elseif (!empty($res['data']['item']['skuProps'][1][0])){
                        for ($i = 1;$i < count($res['data']['item']['sku']);$i++){
                            $stockModel = new SaleGoodsStockModel();
                            $stockData['property1_name'] = $res['data']['item']['sku'][$i][1];
                            $stockData['property2_name'] = '';
                            $stockData['number'] = 0;
                            $stockData['price'] = $res['data']['item']['sku'][$i]['price'];
                            $stockData['pic_url'] = $res['data']['item']['images'][0];
                            $stockModel->do_add($stockData);
                        }
                    } else {
                        $stockData['property1_name'] = '默认';
                        $stockData['property2_name'] = '';
                        $stockData['number'] = 0;
                        if (isset($res['data']['item']['price'])){
                            $stockData['price'] = $res['data']['item']['price'];
                        }else{
                            $stockData['price'] = 0;
                        }
                        $stockData['pic_url'] = $res['data']['item']['images'][0];
                        $stockModel->do_add($stockData);
                    }
                }else{
                    $stockData['property1_name'] = '默认';
                    $stockData['property2_name'] = '';
                    $stockData['number'] = 0;
                    if (isset($res['data']['item']['price'])){
                        $stockData['price'] = $res['data']['item']['price'];
                    }else{
                        $stockData['price'] = 0;
                    }
                    $stockData['pic_url'] = $res['data']['item']['images'][0];
                    $stockModel->do_add($stockData);
                }

            }
            return $array;
        } else {
            return result(500,'商品:'.$itemid.',导入失败');
        }
    }

    function pf1688($key,$apiKey,$itemid){
        $url = "https://api03.6bqb.com/alibaba/detail?apikey={$apiKey}&itemid={$itemid}";
        $res = json_decode(curlGet($url),true);

        if ($res['retcode'] === '0000') {
            $goodsModle = new SaleGoodsModel();
            $goodsData['key'] = $key;
            $goodsData['merchant_id'] = yii::$app->session['uid'];
            $goodsData['name'] = $res['data']['title'];
            $goodsData['attribute'] = [];
            $goodsData['attribute'] = json_encode($goodsData['attribute']);
            $goodsData['stocks'] = 0;
            if (isset($res['data']['images']) && is_array($res['data']['images'])){
                $imgNum = count($res['data']['images']);
                $imgNum = $imgNum > 5 ? 5 : $imgNum;
                $goodsData['pic_urls'] = '';
                for ($i = 0;$i < $imgNum;$i++) {
                    if ($i == 0) {
                        $goodsData['pic_urls'] .= $res['data']['images'][$i];
                    } else {
                        $goodsData['pic_urls'] .= ','.$res['data']['images'][$i];
                    }
                }
            }
            if (isset($res['data']['skuProps']) && is_array($res['data']['skuProps'])) {
                $specsNum = count($res['data']['skuProps']);
                if ($specsNum == 1) { //单规格
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['skuProps'][0]['prop'].':';
                    $goodsData['property2'] = '';
                    for ($i = 0;$i < count($res['data']['skuProps'][0]['value']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['skuProps'][0]['value'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['skuProps'][0]['value'][$i]['name'];
                        }
                    }
                    if (isset($res['data']['skuMap']) && is_array($res['data']['skuMap'])){
                        foreach ($res['data']['skuMap'] as $k=>$v){
                            $goodsData['stocks'] = $goodsData['stocks'] + $v['canBookCount'];
                        }
                    }
                } elseif ($specsNum == 2) { //双规格
                    $goodsData['stock_type'] = 2;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = $res['data']['skuProps'][0]['prop'].':';
                    for ($i = 0;$i < count($res['data']['skuProps'][0]['value']);$i++) {
                        if ($i == 0) {
                            $goodsData['property1'] .= $res['data']['skuProps'][0]['value'][$i]['name'];
                        } else {
                            $goodsData['property1'] .= ','.$res['data']['skuProps'][0]['value'][$i]['name'];
                        }
                    }
                    $goodsData['property2'] = $res['data']['skuProps'][1]['prop'].':';
                    for ($i = 0;$i < count($res['data']['skuProps'][1]['value']);$i++) {
                        if ($i == 0) {
                            $goodsData['property2'] .= $res['data']['skuProps'][1]['value'][$i]['name'];
                        } else {
                            $goodsData['property2'] .= ','.$res['data']['skuProps'][1]['value'][$i]['name'];
                        }
                    }
                    if (isset($res['data']['skuMap']) && is_array($res['data']['skuMap'])){
                        foreach ($res['data']['skuMap'] as $k=>$v){
                            $goodsData['stocks'] = $goodsData['stocks'] + $v['canBookCount'];
                        }
                    }
                } else {
                    $goodsData['stock_type'] = 1;
                    $goodsData['have_stock_type'] = 1;
                    $goodsData['property1'] = '默认:默认';
                    $goodsData['property2'] = '';
                }
            }else{
                $goodsData['stock_type'] = 1;
                $goodsData['have_stock_type'] = 1;
                $goodsData['property1'] = '默认:默认';
                $goodsData['property2'] = '';
            }
            if (isset($res['data']['descImgs']) && is_array($res['data']['descImgs'])){
                $goodsData['detail_info'] = '<p>';
                foreach ($res['data']['descImgs'] as $k=>$v){
                    $goodsData['detail_info'] .= "<img class='wscnph' style='width: 100%; vertical-align: middle;' src='{$v}' />";
                }
                $goodsData['detail_info'] .= '</p>';
            }else{
                $goodsData['detail_info'] = '<p>   </p>';
            }
            if (isset($res['data']['displayPrice'])){
                $price = explode("-", str_replace('￥','',$res['data']['displayPrice']));
                $goodsData['price'] = is_array($price) ? $price[0] : str_replace('￥','',$res['data']['displayPrice']);
            }else{
                $goodsData['price'] = 0;
            }
            $array = $goodsModle->do_add($goodsData);
            if ($array['status'] == 200){
                $stockModel = new SaleGoodsStockModel();
                $stockData['key'] = $key;
                $stockData['merchant_id'] = yii::$app->session['uid'];
                $stockData['goods_id'] = $array['data'];
                $stockData['name'] = $res['data']['title'];
                $stockData['status'] = 1;
                if (isset($res['data']['skuProps']) && is_array($res['data']['skuProps'])){
                    if ($specsNum == 1) {
                        foreach ($res['data']['skuProps'][0]['value'] as $k=>$v){
                            $stockModel = new SaleGoodsStockModel();
                            $stockData['property1_name'] = $v['name'];
                            $stockData['property2_name'] = '';
                            if (isset($res['data']['skuMap'][$v['name']]['canBookCount'])){
                                $stockData['number'] = $res['data']['skuMap'][$v['name']]['canBookCount'];
                            } else {
                                $stockData['number'] = 0;
                            }
                            if (isset($res['data']['skuMap'][$v['name']]['price'])){
                                $stockData['price'] = $res['data']['skuMap'][$v['name']]['price'];
                            } else {
                                $stockData['price'] = 0;
                            }
                            $stockData['pic_url'] = isset($v['imageUrl']) ? $v['imageUrl'] : $res['data']['images'][0];
                            $stockModel->do_add($stockData);
                        }
                    } elseif ($specsNum == 2) {
                        foreach ($res['data']['skuProps'][0]['value'] as $k=>$v){
                            foreach ($res['data']['skuProps'][1]['value'] as $tk=>$tv){
                                $stockModel = new SaleGoodsStockModel();
                                $stockData['property1_name'] = $v['name'];
                                $stockData['property2_name'] = $tv['name'];
                                $KeyValue = $v['name'].'&gt;'.$tv['name'];
                                if (isset($res['data']['skuMap'][$KeyValue]['canBookCount'])){
                                    $stockData['number'] = $res['data']['skuMap'][$KeyValue]['canBookCount'];
                                } else {
                                    $stockData['number'] = 0;
                                }
                                if (isset($res['data']['skuMap'][$KeyValue]['price'])){
                                    $stockData['price'] = $res['data']['skuMap'][$KeyValue]['price'];
                                } else {
                                    $stockData['price'] = 0;
                                }
                                $stockData['pic_url'] = isset($v['imageUrl']) ? $v['imageUrl'] : $res['data']['images'][0];
                                $stockModel->do_add($stockData);
                            }
                        }
                    } else {
                        $stockData['property1_name'] = '默认';
                        $stockData['property2_name'] = '';
                        $stockData['number'] = 0;
                        if (isset($res['data']['displayPrice'])){
                            $price = explode("-", str_replace('￥','',$res['data']['displayPrice']));
                            $stockData['price'] = is_array($price) ? $price[0] : str_replace('￥','',$res['data']['displayPrice']);
                        }else{
                            $stockData['price'] = 0;
                        }
                        $stockData['pic_url'] = $res['data']['images'][0];
                        $stockModel->do_add($stockData);
                    }
                }else{
                    $stockData['property1_name'] = '默认';
                    $stockData['property2_name'] = '';
                    $stockData['number'] = 0;
                    if (isset($res['data']['displayPrice'])){
                        $price = explode("-", str_replace('￥','',$res['data']['displayPrice']));
                        $stockData['price'] = is_array($price) ? $price[0] : str_replace('￥','',$res['data']['displayPrice']);
                    }else{
                        $stockData['price'] = 0;
                    }
                    $stockData['pic_url'] = $res['data']['images'][0];
                    $stockModel->do_add($stockData);
                }

            }
            return $array;
        } else {
            return result(500,'商品:'.$itemid.',导入失败');
        }
    }



}
